
import dialog.BeautyContestOpt;
import dialog.CentipedeOpt;
import dialog.CentipedePlayer2Opt;
import dialog.DictatorOpt;
import dialog.ImpulseOpt;
import dialog.RiskPrefOpt;
import dialog.TrustOpt;
import dialog.TwoByTwoMatrixOpt;
import dialog.UltimatumOpt;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.Reader;
import java.util.Vector;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfigScreen.java
 *
 * Created on Mar 1, 2009, 2:09:13 PM
 */

/**
 *
 * @author Vinay
 */
public class ConfigScreen extends javax.swing.JFrame
{
    static Vector<String> gameParamList = new Vector();
    static Game g = new Game();

    /** Creates new form ConfigScreen */
    public ConfigScreen() {
        initComponents();
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtAge = new javax.swing.JTextField();
        txtLname = new javax.swing.JTextField();
        txtOccup = new javax.swing.JTextField();
        txtFname = new javax.swing.JTextField();
        lblLname = new javax.swing.JLabel();
        lblFname = new javax.swing.JLabel();
        lblAge = new javax.swing.JLabel();
        lblOccup = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnDefault = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstTo = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstFrom = new javax.swing.JList();
        btnStart = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        txtAge.setFont(new java.awt.Font("Tahoma", 0, 13));
        txtAge.setPreferredSize(new java.awt.Dimension(70, 30));

        txtLname.setFont(new java.awt.Font("Tahoma", 0, 13));
        txtLname.setPreferredSize(new java.awt.Dimension(70, 30));

        txtOccup.setFont(new java.awt.Font("Tahoma", 0, 13));
        txtOccup.setPreferredSize(new java.awt.Dimension(70, 30));

        txtFname.setFont(new java.awt.Font("Tahoma", 0, 13));
        txtFname.setPreferredSize(new java.awt.Dimension(70, 30));

        lblLname.setFont(new java.awt.Font("Tahoma", 0, 13));
        lblLname.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLname.setText("Last Name");
        lblLname.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblLname.setPreferredSize(new java.awt.Dimension(70, 30));

        lblFname.setFont(new java.awt.Font("Tahoma", 0, 13));
        lblFname.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblFname.setText("First Name");
        lblFname.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblFname.setPreferredSize(new java.awt.Dimension(70, 30));

        lblAge.setFont(new java.awt.Font("Tahoma", 0, 13));
        lblAge.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblAge.setText("Age");
        lblAge.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblAge.setPreferredSize(new java.awt.Dimension(70, 30));

        lblOccup.setFont(new java.awt.Font("Tahoma", 0, 13));
        lblOccup.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblOccup.setText("Occupation");
        lblOccup.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lblOccup.setPreferredSize(new java.awt.Dimension(70, 30));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(92, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblFname, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFname, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(78, 78, 78)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblLname, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLname, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(62, 62, 62)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblAge, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(62, 62, 62)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblOccup, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtOccup, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(74, 74, 74))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(lblLname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtLname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(lblAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(lblOccup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtOccup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblFname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {lblAge, lblFname, lblLname, lblOccup, txtAge, txtFname, txtLname, txtOccup});

        btnDefault.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnDefault.setText("Set Default");
        btnDefault.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDefaultActionPerformed(evt);
            }
        });

        btnRemove.setText("<<");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        lstTo.setFont(new java.awt.Font("Tahoma", 0, 13));
        lstTo.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lstTo);

        btnAdd.setText(">>");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        lstFrom.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        lstFrom.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Impulse", "2by2-Matrix", "Beauty-Contest", "Risk-Preference", "Centipede", "Centipede-Player2", "Trust", "Trust-Player2", "Dictator", "Ultimatum", "Ultimatum-Player2" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstFrom.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstFrom);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnDefault)
                    .addComponent(btnAdd)
                    .addComponent(btnRemove))
                .addGap(63, 63, 63)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(60, Short.MAX_VALUE)
                .addComponent(btnAdd)
                .addGap(29, 29, 29)
                .addComponent(btnRemove)
                .addGap(35, 35, 35)
                .addComponent(btnDefault)
                .addGap(52, 52, 52))
        );

        btnStart.setFont(new java.awt.Font("Tahoma", 0, 14));
        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(105, 105, 105)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(344, 344, 344))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(55, 55, 55)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52)
                .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(74, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private String getGameParam(String game)
    {
        if(game.equals("Impulse"))
        {
           dialog.ImpulseOpt dBox = new ImpulseOpt(this, true);
           dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("2by2-Matrix"))
        {
            dialog.TwoByTwoMatrixOpt dBox = new TwoByTwoMatrixOpt(this, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Beauty-Contest"))
        {
            dialog.BeautyContestOpt dBox = new BeautyContestOpt(this, true, false);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Risk-Preference"))
        {
            dialog.RiskPrefOpt dBox = new RiskPrefOpt(this, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Risk-Preference"))
        {
            dialog.RiskPrefOpt dBox = new RiskPrefOpt(this, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Centipede"))
        {
            dialog.CentipedeOpt dBox = new CentipedeOpt(this, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Centipede-Player2"))
        {
            dialog.CentipedePlayer2Opt dBox = new CentipedePlayer2Opt(this, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Trust"))
        {
            dialog.TrustOpt dBox = new TrustOpt(this, true, false);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Trust-Player2"))
        {
            dialog.TrustOpt dBox = new TrustOpt(this, true, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Dictator"))
        {
            dialog.DictatorOpt dBox = new DictatorOpt(this, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Ultimatum"))
        {
            dialog.UltimatumOpt dBox = new UltimatumOpt(this, true, false);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else if(game.equals("Ultimatum-Player2"))
        {
            dialog.UltimatumOpt dBox = new UltimatumOpt(this, true, true);
            dBox.setVisible(true);
           return dBox.getParamsList();
        }
        else
        {
            return "NULL";
        }
    }

    public Vector<String> getGameParamsList()
    {
        return gameParamList;
    }

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:

        String game = lstFrom.getSelectedValue().toString();
        String params = getGameParam(game);
        gameParamList.add(game+","+params);
        lstTo.setListData(gameParamList);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
        // TODO add your handling code here:
        if(!lstTo.isSelectionEmpty())
        {
            gameParamList.remove(lstTo.getSelectedIndex());
            lstTo.setListData(gameParamList);
        }
    }//GEN-LAST:event_btnRemoveActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        // TODO add your handling code here:
        if(gameParamList.size()==0)
        {
            JOptionPane.showMessageDialog(this, "No Games have been selected","",JOptionPane.WARNING_MESSAGE);
        }
        else
        {
            this.setVisible(false);
            g.free=true;
            //startGames();
        }
}//GEN-LAST:event_btnStartActionPerformed

    private void btnDefaultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDefaultActionPerformed
        // TODO add your handling code here:
        setDefault();
    }//GEN-LAST:event_btnDefaultActionPerformed

    private void setDefault() {
        try {
            gameParamList.removeAllElements();
            BufferedReader br = new BufferedReader(new FileReader("list.txt"));
            String line = null;

            while ((line = br.readLine()) != null) {
                gameParamList.add(line);
            }
            lstTo.setListData(gameParamList);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConfigScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDefault;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnStart;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblAge;
    private javax.swing.JLabel lblFname;
    private javax.swing.JLabel lblLname;
    private javax.swing.JLabel lblOccup;
    private javax.swing.JList lstFrom;
    private javax.swing.JList lstTo;
    private javax.swing.JTextField txtAge;
    private javax.swing.JTextField txtFname;
    private javax.swing.JTextField txtLname;
    private javax.swing.JTextField txtOccup;
    // End of variables declaration//GEN-END:variables

}
